package com.franklin.icharm;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gm.contentprovider.GmailContract;
import com.google.android.gm.contentprovider.GmailContract.Labels;

public class sClockEmail {
	public  static final String TAG = "smartclock";

	private static final String ACCOUNT_TYPE_GOOGLE = "com.google";
	private static final String[] FEATURES_MAIL = {"service_mail"};
	private Context mContext;

	static final String[] COLUMNS_TO_SHOW = new String[] {
		GmailContract.Labels.NAME,
		GmailContract.Labels.URI,
		GmailContract.Labels.NUM_UNREAD_CONVERSATIONS,
		GmailContract.Labels.NUM_CONVERSATIONS
	};

	public sClockEmail(Context context)
	{
		mContext = context;
	}

	public void startAccountList() {
		// Get the account list, and pick the first one
		AccountManager.get(mContext).getAccountsByTypeAndFeatures(ACCOUNT_TYPE_GOOGLE, FEATURES_MAIL,
				new AccountManagerCallback<Account[]>() {
					@Override
					public void run(AccountManagerFuture<Account[]> future) {
						Account[] accounts = null;
						try {
							accounts = future.getResult();
						} catch (OperationCanceledException oce) {
							Log.e(TAG, "Got OperationCanceledException", oce);
						} catch (IOException ioe) {
							Log.e(TAG, "Got OperationCanceledException", ioe);
						} catch (AuthenticatorException ae) {
							Log.e(TAG, "Got OperationCanceledException", ae);
						}
						onAccountResults(accounts);
					}
				}, null /* handler */);
	}

	private void onAccountResults(Account[] accounts) {
		Log.i(TAG, "received accounts: " + Arrays.toString(accounts));
		if (accounts != null && accounts.length > 0) {
			// Pick the first one, and display a list of labels
			final String account = accounts[0].name;
			Log.i(TAG, "Starting loader for labels of account: " + account);
			final Bundle args = new Bundle();
			args.putString("account", account);
			//getSupportLoaderManager().restartLoader(0, args, this);
		}
	}



	public static int getUnReadCount(Uri uri , Context context)
	{
		final Uri labelUri = uri;
		int totalUnread = 0;
		try {
			ContentResolver contentResolver= context.getContentResolver();
			Cursor cursor = contentResolver.query(labelUri, null, null, null, null);
			//iterate over all labels in the account

			if (cursor.moveToFirst()) {
				int unreadColumn = cursor.getColumnIndex(Labels.NUM_UNREAD_CONVERSATIONS);

				int nameColumn = cursor.getColumnIndex(Labels.NAME);
				int laberuri = cursor.getColumnIndex("labelUri");
				int subCol = cursor.getColumnIndex("subject");
				for (int i= 0 ; i < cursor.getColumnCount(); i++)
					//Log.d(TAG,"Colnames : "+ cursor.getColumnName(i));
				do {

					String name = cursor.getString(nameColumn);
					String unread = cursor.getString(unreadColumn);
					String luri = cursor.getString(laberuri);
					if (subCol >= 0 ){
						String subject = cursor.getString(subCol);
						//Log.d(TAG,"@_@EMAIL: (sub)" + subject );

					}
					//Log.d(TAG,"@_@EMAIL: " + name + " " + unread + " " + luri);
					try {
						int iunread = Integer.parseInt(unread);
						totalUnread += iunread;
					}catch (NumberFormatException numEx )
					{

					}
					break;  // only read Primary email box
				} while (cursor.moveToNext());
			}
		}catch (Exception ex)
		{
			Log.d(TAG, ex.getLocalizedMessage());
		}
		return totalUnread;
	}


	
	public static ArrayList<String> getUnReadSubjects(Uri uri, Context context)
	{
		ArrayList<String> subjects = new ArrayList<String>();
		try{
			ContentResolver cr = context.getContentResolver();
			Cursor unread = cr.query(uri, null, null, null, null);
			unread.moveToFirst();
			int subjectIdx = unread.getColumnIndex("subject");

			do {
				String subject = unread.getString(subjectIdx);
				subjects.add(subject);

			} while (unread.moveToNext());
		}catch (Exception ex)
		{
			Log.d(TAG, ex.getLocalizedMessage());
		}
		return subjects;
	}

}
